package com.wzdigit.wms.wms.service.order;

import com.wzdigit.wms.basic.domain.dtos.transfer.TransferDtlDto;
import com.wzdigit.wms.basic.domain.dtos.transfer.TransferDto;
import com.wzdigit.wms.basic.domain.transfer.TransferDtl;
import com.wzdigit.wms.basic.domain.transfer.TransferDtlGoods;
import com.wzdigit.wms.basic.domain.transfer.TransferHeader;
import com.wzdigit.wms.wms.service.core.transfer.TransferDtlGoodsService;
import com.wzdigit.wms.wms.service.core.transfer.TransferDtlService;
import com.wzdigit.wms.wms.service.core.transfer.TransferHeaderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * <p>
 * 调拨单 服务类
 * </p>
 *
 * @author
 * @since 2021-01-12
 */
@Service
@Slf4j
public class TransferService {

    @Autowired
    @Lazy
    TransferHeaderService transferHeaderService;

    @Autowired
    @Lazy
    TransferDtlService transferDtlService;

    @Autowired
    @Lazy
    TransferDtlGoodsService transferDtlGoodsService;


    /**
     * 生成调拨单:库存调拨单表头、调拨单明细、调拨单单货品清单
     *
     * @return
     */
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public TransferDto addNewOne(TransferDto transferDto) {

        //库存调拨单表头
        TransferHeader transferHeader = transferDto.getTransferHeader();
        transferHeader = transferHeaderService.addNewOne(transferHeader);

        //新增 库存调拨单表头的【单头ID】
        final Integer transferHeaderId = transferHeader.getTransferHeaderId();

        //调拨单明细
        List<TransferDtlDto> transferDtlDtoList = transferDto.getTransferDtlDtoList();

        for (TransferDtlDto transferDtlDto : transferDtlDtoList) {
            //设置单头ID
            TransferDtl transferDtl = transferDtlDto.getTransferDtl();
            transferDtl.setTransferHeaderId(transferHeaderId);
            //新增明细
            transferDtl = transferDtlService.addNewOne(transferDtl);
            transferDtlDto.setTransferDtl(transferDtl);

            //新增【调拨单单货品清单】
            List<TransferDtlGoods> transferDtlGoods = transferDtlDto.getTransferDtlGoods();

            //循环设置明细ID
            for (TransferDtlGoods transferDtlGood : transferDtlGoods) {
                transferDtlGood.setTransferDtlId(transferDtl.getTransferDtlId());
            }

            if (transferDtlGoods !=null && transferDtlGoods.size()>0) {
                List<TransferDtlGoods> transferDtlGoodsList = transferDtlGoodsService.addNewList(transferDtlGoods);
//                transferDto.setTransferDtlDtoList(transferDtlDtoList);
                transferDtlDto.setTransferDtlGoods(transferDtlGoodsList);
            }
        }
        //设置回参
        transferDto.setTransferHeader(transferHeader).setTransferDtlDtoList(transferDtlDtoList);

        return transferDto;
    }


    /**
     * 更新调拨单:库存调拨单表头、调拨单明细、调拨单单货品清单
     *
     * @return
     */
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public TransferDto updateOne(TransferDto transferDto) {

        //库存调拨单表头
        TransferHeader transferHeader = transferDto.getTransferHeader();
        transferHeader = transferHeaderService.updateOne(transferHeader);

//        //调拨单明细
//        TransferDtl transferDtl = transferDto.getTransferDtl();
//        transferDtl = transferDtlService.updateOne(transferDtl);
//
//        //调拨单单货品清单
//        TransferDtlGoods transferDtlGoods = transferDto.getTransferDtlGoods();
//        if (transferDtlGoods != null) {
//            transferDtlGoods = transferDtlGoodsService.updateOne(transferDtlGoods);
//        }
//
//        //结果汇总
//        transferDto.setTransferHeader(transferHeader).setTransferDtl(transferDtl).setTransferDtlGoods(transferDtlGoods);


//        Result<TransferRequest> result = new Result<>();
//        result.setData(transferRequest);
        return transferDto;
    }


    /**
     * 更新调拨单明细的下发拣货任务标识
     * @param docDtlId
     * @param picktaskFlag
     * @param loginName
     * @return
     */
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public Integer updateDtlPicktaskFlag(Long docDtlId, int picktaskFlag, String loginName) {
        Integer row = transferDtlService.updateDtlPicktaskFlag(docDtlId, picktaskFlag, loginName);
        return row;
    }
}
